package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Side;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.net.URL;
import java.util.function.Consumer;

/**
 * @ClassName PieChartDemo
 * @Description
 * @Author wangss
 * @date 2020.06.28 00:23
 * @Version 1.0
 */
public class PieChartDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        PieChart.Data d1 = new PieChart.Data("data1", 40);
        PieChart.Data d2 = new PieChart.Data("data2", 10);
        PieChart.Data d3 = new PieChart.Data("data3", 60);
        PieChart.Data d4 = new PieChart.Data("data4", 100);

        ObservableList<PieChart.Data> list = FXCollections.observableArrayList();
        list.addAll(d1, d2, d3, d4);

        PieChart pieChart = new PieChart(list);
        // 线得长度
//        pieChart.setLabelLineLength(20);
//        pieChart.setLabelsVisible(true);
        // 旋转
//        pieChart.setStartAngle(20);
//        pieChart.setClockwise(true);

//        pieChart.setLegendVisible(false);
        pieChart.setLegendSide(Side.RIGHT);

        pieChart.setAnimated(true);

        pieChart.getData().forEach(new Consumer<PieChart.Data>() {
            @Override
            public void accept(PieChart.Data data) {
                // 每一块区域的模板
                Node node = data.getNode();
                Tooltip tooltip = new Tooltip("数据 : " + data.getPieValue());
                tooltip.setFont(new Font(20));
                Tooltip.install(node, tooltip);

                data.pieValueProperty().addListener(new ChangeListener<Number>() {
                    @Override
                    public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                        Tooltip tooltip1 = new Tooltip("数据 : " + newValue);
                        tooltip1.setFont(new Font(20));
                        Tooltip.install(node, tooltip1);
                    }
                });

                node.setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        System.out.println(data.getName());
                    }
                });
            }
        });

        Button button = new Button("点击");

        AnchorPane root = new AnchorPane();
        root.getChildren().addAll(button, pieChart);
        AnchorPane.setTopAnchor(pieChart, 100d);
        AnchorPane.setLeftAnchor(pieChart, 100d);

        Scene scene = new Scene(root);
        URL url = getClass().getResource("/css/mycss.css");
        scene.getStylesheets().add(url.toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.setTitle("位移");
        primaryStage.setHeight(800d);
        primaryStage.setWidth(800d);
        primaryStage.show();

        button.setOnAction(event -> {
            d1.setPieValue(200);
        });
    }
}
